#include<bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
vector<vector<int>> vec;
vector<vector<int>> operator*(const vector<vector<int>> &v1,const vector<vector<int>> &v2){
    int n = v1.size();
    vector<vector<int>> res(n,vector<int>(n));
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            for(int k = 0;k < n;k++){
                res[i][j] = (res[i][j] + 1ll*v1[i][k]*v2[k][j]%MOD)%MOD;
            }
        }
    }
    return res;
}
vector<vector<int>> operator+(vector<vector<int>> v1,const vector<vector<int>> &v2){
    int n = v1.size();
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            v1[i][j] = (v1[i][j] + v2[i][j])%MOD;
        }
    }
    return v1;
}
vector<vector<int>> operator*(vector<vector<int>> v1,int x){
    int n = v1.size();
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            v1[i][j] = 1ll*v1[i][j]*x%MOD;
        }
    }
    return v1;
}
int main(){
    int n;
    cin>>n;
    vector<vector<int>> vec(n,vector<int>(n));
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            cin>>vec[i][j];
        }
    }
    vector<vector<int>> res(n,vector<int>(n));
    for(int i = 0;i < n;i++){
        res[i][i] = 1;
    }
    int m;
    cin>>m;
    vector<vector<int>> res1(n,vector<int>(n));
    for(int i = 1;i<=m;i++){
        int x;
        cin>>x;
        res1 = res1 + res*x;
        res = res*vec;
    }
    bool val = 0;
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            val |= res1[i][j];
        }
    }
    cout<<val<<'\n';
}